<?php
namespace addons\TfFutures\common\models;

use addons\TfFutures\common\models\Order as OrderModel;
use addons\TfFutures\services\OrderService;
use common\models\common\PayLog;
use common\models\member\Member as Member2;
use EasyWeChat\Kernel\Exceptions\DecryptException;
use yii\db\ActiveRecord;
/**
 * 用户应用信息表.
 */
class Member extends ActiveRecord
{

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%addon_tf_futures_member}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['merchant_id', 'member_id'], 'required'],
            ['lever','integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id'=>'用户',
            'symbol' => '自选',
            'price' => '余额',
            'price_blocked' => '冻结金额',
            'price_occupy' => '占用金额',
            'price_profit' => '总盈利',
            'price_loss' => '总亏损',
            'lever' => '用户杠杆',
            'commission' => '佣金',
            'commission_all' => '总累计佣金'
        ];
    }
    public function getMember_info(){
        return $this->hasOne(Member2::class,['id'=>'member_id']);
    }

    /**
     * 查询个人信息以及个人资产
     * @param $id
     * @return array
     */
    public static function member($id){
        $member = Member2::findOne($id);
        $model = self::find()->where(['member_id'=>$id])->one();
        if (empty($model)) {
            $model = new self();
            $model->member_id = $member->id;
            $model->merchant_id = $member->merchant_id;
            $model->save();
        }
        return [$member,$model];
    }

    /**
     * 用户资金明细
     */
    public static function Detail($member_id)
    {
        //爆仓比列
        $setup  = Setup::findOne(1);
        list($member, $model) = self::member($member_id);
        //用户出金总额
        $money_out = PayLog::find()
            ->where(['member_id' => $member_id,'status' => 1,'order_group' => 'out'])
            ->select('sum(pay_fee) as pay_fee')
            ->one();
         //用户入金总额
        $money_in = PayLog::find()
            ->where(['member_id' => $member_id,'status' => 1,'order_group' => 'in'])
            ->select('sum(pay_fee) as pay_fee')
            ->one();
        //用户当天入金总额
        $money_in_day = PayLog::find()
            ->where(['member_id' => $member_id,'status' => 1,'order_group' => 'in'])
            ->andWhere(['>=','updated_at',strtotime(date("Y-m-d"),time())])
            ->select('sum(pay_fee) as pay_fee')
            ->asArray()
            ->one();
        //用户平仓保证金
        $order_ke = OrderModel::find()
            ->where([
                'member_id' => $member_id,
                'state' => '平仓',
                'status' => 1,
            ])->andWhere(['>=','open_time',date('Y-m-d').' 00:00:00'])->select('sum(service_fee) as service_fee, sum(overfee) as overfee, sum(deposit) as deposit, sum(profit) as profit')
            ->one();
        //用户持仓保证金
        $order_chi = OrderModel::find()
            ->where([
                'member_id' => $member_id,
                'state' => '持仓',
                'status' => 1,
            ])->select('sum(service_fee) as service_fee, sum(overfee) as overfee, sum(deposit) as deposit, sum(profit) as profit')
            ->one();

        //用户当前权益
        $result['money_zong'] = bcadd(bcadd($model->price,$order_chi->deposit,4),$order_chi->profit,4);
        //用户可用资金
        $result['money_yes'] = $model->price;

        //用户风险率
        $result['riskrate'] = bcmul(bcdiv($model->price_occupy,bcadd($model->price_occupy,$model->price,2),2),100,2).'%';
//        echo $model->price_occupy.'/';echo $model->price;die;
//        print_r($result['riskrate']);die;
        //平仓盈亏
        $result['order_in'] = empty(bcadd($model->price_profit,$model->price_loss,2)) ? 0.00 : bcadd($model->price_profit,$model->price_loss,2);
        //持仓盈亏
        $result['order_out'] = empty($order_chi->profit) ? 0.00 : $order_chi->profit;
        //持仓保证金
        $result['deposit'] = empty($order_chi->deposit) ? 0.00 : $order_chi->deposit;
        //持仓手续费
        $result['service_fee'] = empty($order_chi->profi) ? 0.00 : $order_chi->profit;
        //冻结手续费
        $result['service_fee_no'] = '0';
        //用户总入出金
        $result['money'] = empty(bcadd($money_out->pay_fee,$money_in->pay_fee,4))
                                        ? 0.00
                                        : $money_out->pay_fee;
        //用户可用资金显示
        $result['money_ke'] = bcadd($model->price, $result['order_out'],4) < 0
                                        ? 0.00
                                        : bcadd($model->price,$order_chi->profit,4);
        //用户可取资金
        $result['money_qu'] = bcsub($result['money_ke'],abs($order_ke->profit),4) < 0
                                        ? 0.00
                                        : bcsub($result['money_ke'],abs($order_ke->profit),4);
        //用户可取资金减去今天充值金额=可提现金额
        $result['money_qu'] = bcsub($result['money_qu'],$money_in_day->pay_fee,4) < 0
                                        ? 0.00
                                        : bcsub($result['money_qu'],$money_in_day->pay_fee,4);
        //用户名
        $result['username'] = $member->username;
        return $result;
    }
}
